/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.mod.counterclock.statelisteners;

import com.navid.trafalgar.manager.states.AbortedState;
import com.navid.trafalgar.manager.states.PrestartState;
import com.navid.trafalgar.manager.states.StartedState;
import com.navid.trafalgar.manager.states.SuccessfulState;
import com.navid.trafalgar.manager.GameConfiguration;
import com.navid.trafalgar.manager.GameStatus;
import com.navid.trafalgar.persistence.CandidateRecord;
import com.navid.trafalgar.persistence.RecordPersistenceService;
import com.navid.trafalgar.persistence.RecordPersistenceServiceFactory;
import com.navid.trafalgar.persistence.StepRecord;

/**
 *
 * @author alberto
 */
public class GameRecorder implements StartedState, PrestartState, SuccessfulState, AbortedState{
    GameStatus gameStatus;
    GameConfiguration gameConfiguration;
    
    CandidateRecord candidateRecord;
    RecordPersistenceService persistenceService = RecordPersistenceServiceFactory.getFactory(RecordPersistenceServiceFactory.Type.LOCAL);

    public GameRecorder(GameStatus gameStatus, GameConfiguration gameConfiguration) {
        this.gameStatus = gameStatus;
        this.gameConfiguration = gameConfiguration;
    }

    @Override
    public void onPrestart(float tpf) {
        candidateRecord = gameStatus.getMillestoneGameModel().getShip().getCandidateRecordInstance();
        candidateRecord.setMap(gameConfiguration.getMap());
    }

    @Override
    public void onStarted(float tpf) {
        StepRecord newRecord = gameStatus.getMillestoneGameModel().getShip().getSnapshot();
        newRecord.setTimestamp(gameStatus.getTime());
        candidateRecord.addStepRecord(newRecord);
    }

    @Override
    public void onSuccess(float tpf) {
        persistenceService.addCandidate(candidateRecord);
    }
    
    @Override
    public void onAborted(float tpf){
        candidateRecord = null;
    }

    public void onUnload() {
        
    }
    
}
